package com.ccp.dev.qbdms.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.base.BaseController;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.model.WQbdFlightJc;
import com.ccp.dev.qbdms.model.WQbdQualityAnalysisReport;
import com.ccp.dev.qbdms.service.WQbdFlightJcService;
import com.ccp.dev.qbdms.service.WQbdQualityAnalysisReportService;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping(ZhiLiangConst.URL_API + ZhiLiangConst.URL_VERSION)
public class WQbdFlightJcApiController extends BaseController {

    @Resource
    private WQbdFlightJcService wQbdFlightJcService;

    /**
     * 飞行试验奖惩计划下发的接口
     *
     * @param jsonstr
     * @return
     */
    @PostMapping("/wQbdFlightJc/reportSend")
    public Object reportSend(@RequestBody String jsonstr) {
        //解析json
        JSONObject jsonobj = JSONObject.parseObject(jsonstr);
        // 获取上报参数,是一个list
        JSONArray listobj = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST);
        List<WQbdFlightJc> list = listobj.toJavaList(WQbdFlightJc.class);
        boolean hasFile = jsonobj.containsKey(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
        List<ExtendSysFile> filelist= Lists.newArrayList();
        if(hasFile){
            JSONArray filelistjson = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
            filelist = filelistjson.toJavaList(ExtendSysFile.class);
            ReadAttachmentUtil.getEntityData(list,filelist);
        }
        int result=0;
        for (WQbdFlightJc wQbdFlightJc:list){
             result=wQbdFlightJcService.saveSendReport(wQbdFlightJc);
        }
        if (result>0) {
            return success("成功");
        }else{
            return fail("失败");
        }
    }
}
